package com.example.api.exception.handler;

import com.example.api.exception.MyException;
import com.example.util.result.RestResult;
import com.example.util.result.RestResultBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 全局异常处理器
 *
 *
 * @RestControllerAdvice 这个注解注解只处理经过controller的异常，不经共controller的异常不处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public RestResult exceptionHandler(Exception e){
        log.error("Catch exception, cause:" + e.getMessage(), e);
        return new RestResultBuilder().fail(e.getMessage());
    }

    /**
     * 自定义异常处理示例
     * @param e
     * @return
     */
    @ExceptionHandler(value = MyException.class)
    public RestResult myExceptionHandler(MyException e){
        log.error("Catch myException, cause:" + e.getMessage(), e);
        return new RestResultBuilder().fail(e.getMessage());
    }
}
